package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.WorkApplyDao;
import com.framework.entity.flow.FlowNoticeTerInfo;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.work.WorkApply;
import com.framework.entity.work.WorkBack;
import com.framework.entity.work.WorkTransfer;
import com.framework.entity.work.WorkUntie;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.service.AgentService;
import com.framework.service.NoticeReadService;
import com.framework.service.TerminalInfoService;
import com.framework.service.WorkApplyService;
import com.framework.utils.string.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author wq
 */
@Service("workApplyService")
public class WorkApplyServiceImpl extends ServiceImpl<WorkApplyDao, WorkApply> implements WorkApplyService {

	@Autowired
	private AgentService agentService;
	@Autowired
	private NoticeReadService noticeReadService;
	@Autowired
	private TerminalInfoService terminalInfoService;

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertTerBack(String token,String[] sns,String backPhone) {
		if(sns.length == 0 || StringUtils.isBlank(backPhone)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		WorkBack workBack = new WorkBack();
		//生成唯一id
		String no = StringUtil.getDateString(4);
		SysAgent agent = agentService.queryAgentByToken(token);

		SysAgent bankAgent=agentService.queryByMobileAndDeptId(backPhone,agent.getDeptId());
		if(bankAgent==null){
			throw new RRException("9000","回拨目标不存在,请仔细核对");
		}
		Long agentId = agent.getAgentId();
		workBack.setAgentId(agentId);
		workBack.setIsSolve(1);
		workBack.setBackNo(no);
		workBack.setBackAgentId(bankAgent.getAgentId());
		baseMapper.insertTerBack(workBack);
		//添加回拨操作的机器
		for (int i = 0; i < sns.length; i++) {
			String sn = sns[i];
			//查询终端号是否存在
			TerTerminalInfo terminalInfo = terminalInfoService.queryTerminalBySn(sn);
			if(terminalInfo == null){
				throw new RRException("9000","未查询到("+sn+")所属信息");
			}
			FlowNoticeTerInfo n = new FlowNoticeTerInfo();
			n.setSn(sn);
			n.setCreateTime(new Date());
			n.setNoticeTerNo(no);
			noticeReadService.insertNoticeTerInfo(n);
		}
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertTerTransfer(String token, WorkTransfer workTransfer, String acceptPhone, String thisPhone, Long deptId) {
		if(workTransfer == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		if(StringUtils.isBlank(workTransfer.getSn())){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		//查询终端号是否存在
		TerTerminalInfo terminalInfo = terminalInfoService.queryTerminalBySn(workTransfer.getSn());
		if(terminalInfo == null){
			throw new RRException("9000","未查询到("+workTransfer.getSn()+")所属信息");
		}
		if(deptId==null){
			deptId=1L;
		}

		SysAgent acceptAgent=agentService.queryByMobileAndDeptId(acceptPhone,deptId);
		SysAgent thisAgent=agentService.queryByMobileAndDeptId(thisPhone,deptId);
		if(acceptAgent == null || thisAgent == null){
			throw new RRException("9000","接收人或登录人账号错误");
		}
		workTransfer.setAcceptId(acceptAgent.getAgentId());
		workTransfer.setThisAgentId(thisAgent.getAgentId());
		workTransfer.setCreateTime(new Date());
		baseMapper.insertTerTransfer(workTransfer);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertTerUntie(String token, WorkUntie workUntie) {
		if(workUntie == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		if(StringUtils.isBlank(workUntie.getSn()) ||
				StringUtils.isBlank(workUntie.getMerchantName()) ||
				StringUtils.isBlank(workUntie.getPhone())){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		workUntie.setAgentId(agentId);
		baseMapper.insertTerUntie(workUntie);
	}

	@Override
	public void insertWorkApply(WorkApply workApply, String token, String phone, Long deptId) {
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		if(workApply == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		if(StringUtils.isBlank(phone) ||
				workApply.getTypes() == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		if(deptId==null){
			deptId=1L;
		}
		SysAgent logAgent=agentService.queryByMobileAndDeptId(phone,deptId);
		if(logAgent == null){
			throw new RRException("9000","登录人账号错误");
		}
		workApply.setAgentId(agentId);
		workApply.setCreateTime(new Date());
		workApply.setLogAgentId(logAgent.getAgentId());
		workApply.setIsSolve(1);
		baseMapper.insert(workApply);
	}
}
